//
//  FamilymemberVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/29.
//  Copyright © 2020 tym. All rights reserved.
//

#import "FamilymemberVC.h"
#import "FamilyService.h"
#import "UIView+ZFFrame.h"
#import "FamilyModel.h"
#import "UserModel.h"
#import "ShowAlertView.h"
#import "UIImageView+YPImage.h"

@interface FamilymemberVC ()

@property(nonatomic,weak) IBOutlet UIView *contView;
@property(nonatomic,weak) IBOutlet NSLayoutConstraint *contViewH;
@property(nonatomic,weak) IBOutlet UIImageView *headImage;
@property(nonatomic,weak) IBOutlet UILabel *nameLab;
@property(nonatomic,weak) IBOutlet UILabel *mobileLab;
@property(nonatomic,weak) IBOutlet UIView *mobilView;
@property(nonatomic,weak) IBOutlet UIStackView *bottomView;
@property(nonatomic,weak) IBOutlet UIButton *famliyBtn;

@property(nonatomic,weak) IBOutlet UIView *blackConView;
@property(nonatomic,weak) IBOutlet UISwitch *sithch;
@property(nonatomic,strong) FamilyService *service;
@end

@implementation FamilymemberVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"家庭成员"];
    
    self.headImage.layer.cornerRadius = self.headImage.frame.size.height/2;
    self.headImage.layer.masksToBounds = YES;
    self.sithch.onTintColor = rgba(235, 238, 241);
    self.sithch.tintColor = rgba(235, 238, 241);
    self.sithch.thumbTintColor = rgba(155, 155, 155);
    [self.sithch setOn:NO];
}

-(IBAction)blackSwithAction:(UISwitch*)sender{
    
    [ShowAlertView showAlertViewTitle:@"提示" message:@"加入黑名单后，该用户将不再是家庭成员，确定要加入吗?" actionBtnTitle:@"确定"
                           hiddenCancel:NO
                           completion:^(NSInteger actionType) {
        if (actionType) {
            self.sithch.thumbTintColor = rgba(76, 132, 255);
            [self josinBlockUser];
        }else{
            self.sithch.on = NO;
            self.sithch.thumbTintColor = rgba(155, 155, 155);
        }
    }];
    
}

-(FamilyService*)service{
    if (_service == nil) {
        _service = [[FamilyService alloc] init];
    }
    return _service;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _mobileLab.text = self.userModel.mobile;
    if (self.userModel.nickname) {
        _nameLab.text = self.userModel.nickname;
    }
    if (self.userModel.headImgUrl.length) {
        [self.headImage yp_setImageUlr:[self.userModel getEquipmentImage]];
    }
    
    if (self.userModel.type == 1) { //app用户
        if (self.userModel.familyRole == 1 ||
            [UserTool getCurrentFamily].familyRole == 2) {
            _bottomView.hidden = YES;
            _blackConView.hidden = YES;
        }else{
            _blackConView.hidden = NO;
            _bottomView.hidden = NO;
        }
        self.famliyBtn.hidden = NO;
        //self.blackConView.hidden = NO;
        self.mobilView.hidden = NO;
        self.contViewH.constant = 192;
        if ([self.userModel.mobile isEqualToString:[UserTool getLogInUser].mobile]) {
            self.blackConView.hidden = YES;
        }
    }else{
        self.blackConView.hidden = YES;
        self.famliyBtn.hidden = YES;
        self.mobilView.hidden = YES;
        self.contViewH.constant = 146;
        self.bottomView.hidden = YES;
    }
}

-(void)setUserModel:(UserModel *)userModel{
    _userModel = userModel;
}

-(IBAction)endowFatherAction:(id)sender{
    TYMWeak(self);
    [ShowAlertView showAlertViewTitle:@"提示" message:@"确定授予该成员家长权限吗?" actionBtnTitle:@"确定" hiddenCancel:NO completion:^(NSInteger actionType) {
        if (actionType) {
            [weakself usergrantpatriarch];
        }
    }];
}

-(void)usergrantpatriarch{
    [self.service getFamilyusergrantpatriarch:self.userModel.familyUserId completion:^(NSError * _Nonnull error, id  _Nonnull respons) {
        if(!error){
            [WHToast showMessage:@"授予家长权限成功" duration:2 finishHandler:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    }];
}

-(IBAction)deleteMemeberAction:(id)sender{
    
    TYMWeak(self);
    [ShowAlertView showAlertViewTitle:@"提示" message:@"确定删除该成员吗?" actionBtnTitle:@"确定"
                           hiddenCancel:NO
                           completion:^(NSInteger actionType) {
        if (actionType) {
            [weakself deleteUserAction];
        }
    }];
}

-(void)deleteUserAction{
    TYMWeak(self);
    if (self.userModel.type == 1) { //App用户
        [self.service getFamilyuserdeletefamilyusers:self.userModel.familyUserId completion:^(LMJBaseResponse *responObjec) {
            if(responObjec.code == 0){
                [WHToast showMessage:@"操作成功" duration:2 finishHandler:^{
                    [weakself.navigationController popViewControllerAnimated:YES];
                }];
            }else{
                [ShowAlertView showAlertViewTitle:@"提示" message:responObjec.message actionBtnTitle:@"确定"
                                       hiddenCancel:YES
                                       completion:^(NSInteger actionType) {
                }];
            }
        }];
    }else{  //2:游客用户
        
    }
}

-(void)josinBlockUser{
    
    TYMWeak(self);
    [self.service getUpdatefamilyuserstatus:self.userModel.familyUserId status:3 Completion:^(LMJBaseResponse * _Nonnull responsed) {
        if (responsed.message) {
            weakself.sithch.on = NO;
            weakself.sithch.thumbTintColor = rgba(155, 155, 155);
             [ShowAlertView showAlertViewTitle:@"提示" message:responsed.message actionBtnTitle:@"确定"
                                                  hiddenCancel:YES
                                                  completion:^(NSInteger actionType) {
                           }];
        }else{
            [weakself.navigationController popViewControllerAnimated:YES];
        }
    }];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
